//
//  main.m
//  PrepareForConvertSchema
//
//  Created by Chenly on 14-4-2.
//  Copyright (c) 2014年 Chenly. All rights reserved.
//

#import <Foundation/Foundation.h>

void traverseDirectory(NSDirectoryEnumerator **directoryEnum, NSString *directoryName)
{
    NSMutableArray *files = [NSMutableArray array];
    
    NSString *fileName;
    while (fileName = [*directoryEnum nextObject]) {
        
        BOOL isDirectory;
        [[NSFileManager defaultManager] fileExistsAtPath:fileName isDirectory:&isDirectory];
        
        if (isDirectory) {
            
            traverseDirectory(directoryEnum, [NSString string])
        }
        else if ([[fileName pathExtension] isEqualTo:@"xsd"]) {
            
            [directory setObject:<#(id)#> forKey:<#(id<NSCopying>)#>];
        }

    }
    
    NSEnumerator *fileenum;
    fileenum = [files objectEnumerator];
    
    while (filename = [fileenum nextObject]) {
        NSLog(@"%@", filename);
    }
}

void exitForWrongArgs(NSString *error)
{
    NSLog(@"Error: %@", error);
    exit(1);
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        if (argc < 2) {
            
            exitForWrongArgs(@"缺少参数！");
        }
        else if(argc > 3)
        {
            exitForWrongArgs(@"多余参数！");
        }
        
        NSString *rootPath = [NSString stringWithUTF8String:argv[0]];
        rootPath = [rootPath stringByDeletingLastPathComponent];
        NSString *sourcePath = nil;
        NSString *outputPath = nil;
        
        for (int i = 1; i < argc; i ++) {
            
            NSString *argStr = [NSString stringWithUTF8String:argv[i]];
            
            if ([argStr rangeOfString:@"-out="].location == NSNotFound) {
                
                if (sourcePath) {
                    
                    exitForWrongArgs(@"参数不正确, 检查参数格式");
                }
                else
                {
                    if ([argStr rangeOfString:@"/"].location == 0) {
                        
                        sourcePath = argStr;
                    }
                    else if ([argStr rangeOfString:@".."].location == 0) {
                        
                        sourcePath = [[rootPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[argStr substringFromIndex:3]];
                    }
                    else if ([argStr rangeOfString:@"."].location == 0) {
                        
                        sourcePath = [rootPath stringByAppendingPathComponent:[argStr substringFromIndex:2]];
                    }
                    else
                    {
                        sourcePath = [rootPath stringByAppendingPathComponent:argStr];
                    }
                }
            }
            else
            {
                if (outputPath) {
                    
                    exitForWrongArgs(@"参数不正确, 检查是否存在两个-out");
                }
                else
                {
                    argStr = [argStr substringFromIndex:5];
                    
                    if ([argStr rangeOfString:@"/"].location == 0) {
                        
                        outputPath = argStr;
                    }
                    else if ([argStr rangeOfString:@".."].location == 0) {
                        
                        outputPath = [[rootPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[argStr substringFromIndex:3]];
                    }
                    else if ([argStr rangeOfString:@"."].location == 0) {
                        
                        outputPath = [rootPath stringByAppendingPathComponent:[argStr substringFromIndex:2]];
                    }
                    else
                    {
                        outputPath = [rootPath stringByAppendingPathComponent:argStr];
                    }
                }
            }
        }
        
        if (!outputPath) {
            
            outputPath = rootPath;
        }
        BOOL sourcePathIsDirectory;
        BOOL outputPathIsDirectory;
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        if (![fileManager fileExistsAtPath:sourcePath isDirectory:&sourcePathIsDirectory] || !sourcePathIsDirectory) {
            
            exitForWrongArgs(@"文件源目录错误");
        }
        
        if (![fileManager fileExistsAtPath:outputPath isDirectory:&outputPathIsDirectory] || !outputPathIsDirectory) {
            
            exitForWrongArgs(@"文件输出目录错误");
        }
        
        
        NSMutableDictionary *directoryDictionary = [NSMutableDictionary dictionary];
        
        NSDirectoryEnumerator *rootDirectoryEnum = [fileManager enumeratorAtPath:sourcePath];
        
        NSString *fileName;
        while (fileName = [rootDirectoryEnum nextObject]) {
            
            NSLog(@"%@", fileName);
        }
        
    }
    return 0;
}

